package com.wisdytech.linkdcs.configuration.action;


import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.wisdytech.common.handler.ResourceMatchingService;
import org.apache.coyote.http2.ConnectionSettingsBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.configuration.model.TtEqConfiguration;
import com.wisdytech.linkdcs.configuration.service.ITtEqConfigurationService;
import com.wisdytech.linkdcs.system.model.SysUser;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dlb
 * @since 2018-05-07
 */
@Controller
@RequestMapping("/equipment/configuration")
public class TtEqConfigurationAction {

    @Resource
    private ITtEqConfigurationService ttEqConfigurationService;
    
    @Value("${scada.design-url}")
    private String scadaDesignUrl;
    @Value("${scada.show-url}")
    private String scadaShowUrl;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    

    /**
     *
     * <p>Description: 车间组态</p>
     * @param model model
     * @return String
     */
    @RequestMapping("/shop/index")
    public String shopIndex(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "monitorManage/workshopConfiguration/index";
    }

    /**
     *
     * <p>Description: 产线组态</p>
     * @param model model
     * @return String
     */
    @RequestMapping("/line/index")
    public String lineIndex(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "monitorManage/productionLineConfiguration/index";
    }

    /**
     *
     * <p>Description: 工厂组态</p>
     * @param model model
     * @return String
     */
    @RequestMapping("/factory/index")
    public String factoryIndex(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "monitorManage/factoryAreaConfiguration/index";
    }
    
    /**
     * 组态设计页面
     * @param model
     * @param type
     * @return
     */
    @RequestMapping("/design/index")
    public String configurationDesignIndex(Model model,
    		@RequestParam(value="type",required=false, defaultValue = "0") String type,
    		String fileKey, HttpServletRequest httpRequest) {
    	//添加展开的等级
        this.resourceMatchingService.getResourceName(httpRequest,model);
        this.resourceMatchingService.getTitle(type,model);
        //组态记录id
        model.addAttribute("fileKey", fileKey);
        //组态记录
        Map<String, Object>  configuration =
        		ttEqConfigurationService.getConfigurationById(fileKey);
        
        if(null == configuration) {
        	//错误页面
        	return "login";		
        }
        model.addAttribute("configuration", configuration);
        
        //组态设计地址
        model.addAttribute("url", scadaDesignUrl);
    	
    	return "monitorManage/configurationDesign/index";
    }
    
    @RequestMapping("/monitor/index")
    public String configurationMonitorIndex(Model model,
    		@RequestParam(value="type",required=false, defaultValue = "0") String type, HttpServletRequest httpRequest) {
    	//添加展开的等级
        this.resourceMatchingService.getResourceName(httpRequest,model);
        this.resourceMatchingService.getTitle(type,model);

        //组态设计地址
        model.addAttribute("url", scadaShowUrl);
    	return "monitorManage/realTimeMonitoring/index";
    }
    

    /**
     *
     * @param type 类型
     * @param code 编码
     * @param locationId 关联id
     * @param filter 分页参数
     * @return StandardTableResult
     */
    @RequestMapping(value = "/listConfigurationByFilter/get.mvc")
    @ResponseBody
    public StandardTableResult<Map<String, Object>> listConfigurationByFilter(String type, String code, String factoryId, String locationId, Filter filter) {
        String configurationType = "1";
        PageInfo<Map<String, Object>> page = this.ttEqConfigurationService.listConfigurationByFilter(type,code,factoryId,locationId,filter,configurationType);
        return new StandardTableResult<>(page.getTotal(),page.getList());
    }
    
    
    /**
     * 根据localtionId获取全部的组态记录
     * @param pkId
     * @return
     */
    @RequestMapping(value = "/listAllConfiguration/get.mvc")
    @ResponseBody
    public StandardResult listConfiguration(String pkId){
        String configurationType = "1";
    	List<TtEqConfiguration>  data =ttEqConfigurationService.listConfiguration(pkId,configurationType);
    	if(null == data || data.size()==0) {
    		return StandardResult.getFailResult();
    	}
    	return StandardResult.getSuccessResult(data);
    }
    

    /**
     *
     * @param ttEqConfiguration 实体类
     * @return StandardResult
     */
    @RequestMapping(value = "/saveOrUpdateConfiguration/save.mvc")
    @ResponseBody
    public StandardResult saveOrUpdateConfiguration(TtEqConfiguration ttEqConfiguration) {
        SysUser user = BaseUtil.getCurrentUser();
        ttEqConfiguration.setConfigurationType("1");//2D组态
        this.ttEqConfigurationService.saveOrUpdateConfiguration(ttEqConfiguration,user);
        return StandardResult.getSuccessResult();
    }

    /**
     * 根据pkId查询组态信息
     * @param pkId 设备code
     * @return StandardResult
     */
    @RequestMapping(value = "/getConfigurationById/get.mvc")
    @ResponseBody
    public StandardResult getConfigurationById(String pkId) {
        Map<String, Object> configuration = ttEqConfigurationService.getConfigurationById(pkId);
        return StandardResult.getSuccessResult(configuration);
    }

    /**
     *
     * @param pkIds 主键
     * @return StandardResult
     */
    @RequestMapping(value = "/deleteConfiguration/del.mvc")
    @ResponseBody
    public StandardResult deleteConfiguration(String pkIds) {
        this.ttEqConfigurationService.deleteConfiguration(pkIds);
        return StandardResult.getSuccessResult();
    }
    
   
    
    

}

